//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.IO;
using EffiProz.Core.DataTypes;
using EffiProz.Core.Lib;
using EffiProz.Core.Errors;
using EffiProz.Core.Lib.IO;

namespace EffiProz.Core.RowIO
{

    /**
     * Base class for reading the data for a database row _in different formats.
     * Defines the methods that are independent of storage format and declares
     * the format-dependent methods that subclasses should define.
     *
     * @author sqlbob@users (RMP)
     * @author fredt@users
     * @version 1.7.2
     * @since 1.7.0
     */
    public abstract class RowInputBase : ByteArrayInputStream
    {

        static int NO_POS = -1;

        // fredt - initialisation may be unnecessary as it's done in resetRow()
        protected int filePos = NO_POS;
        protected int size;

        public RowInputBase()
            : this(new byte[4])
        {

        }

        /**
         * Constructor takes a complete row
         */
        public RowInputBase(byte[] buf)
            : base(buf)
        {

            size = buf.Length;
        }

        public int getPos()
        {

            if (filePos == NO_POS)
            {

                //                Trace.printSystemOut(Trace.DatabaseRowInput_getPos);
            }

            return filePos;
        }

        public int getSize()
        {
            return size;
        }

        // fredt@users - comment - methods used for node and type data
        public abstract int readType();

        public abstract String readString();

        // fredt@users - comment - methods used for SQL types
        protected abstract bool checkNull();

        protected abstract String readChar(SqlType type);

        protected abstract int? readSmallint();

        protected abstract int? readInteger();

        protected abstract long? readBigint();

        protected abstract double? readReal();

        protected abstract decimal? readDecimal(SqlType type);

        protected abstract bool? readBoolean();

        protected abstract TimeData readTime(SqlType type);

        protected abstract TimestampData readDate(SqlType type);

        protected abstract TimestampData readTimestamp(SqlType type);


        protected abstract IntervalMonthData readYearMonthInterval(SqlType type);

        protected abstract IntervalSecondData readDaySecondInterval(SqlType type);


        protected abstract Object readOther();

        protected abstract BinaryData readBinary();


        protected abstract BinaryData readBit();

        protected abstract ClobData readClob();

        protected abstract BlobData readBlob();

        /**
         *  reads row data from a stream using the JDBC types in colTypes
         *
         * @param  colTypes
         * @throws  IOException
         * @throws  CoreException
         */
        public virtual Object[] readData(SqlType[] colTypes)
        {

            int l = colTypes.Length;
            Object[] data = new Object[l];
            Object o;
            SqlType type;

            for (int i = 0; i < l; i++)
            {
                if (checkNull())
                {
                    continue;
                }

                o = null;
                type = colTypes[i];

                switch (type.typeCode)
                {

                    case Types.SQL_ALL_TYPES:
                    case Types.SQL_CHAR:
                    case Types.SQL_VARCHAR:
                    case Types.VARCHAR_IGNORECASE:
                        o = readChar(type);
                        break;

                    case Types.TINYINT:
                    case Types.SQL_SMALLINT:
                        o = readSmallint();
                        break;

                    case Types.SQL_INTEGER:
                        o = readInteger();
                        break;

                    case Types.SQL_BIGINT:
                        o = readBigint();
                        break;

                    //fredt although REAL is now Double, it is read / written in
                    //the old format for compatibility
                    case Types.SQL_REAL:
                    case Types.SQL_FLOAT:
                    case Types.SQL_DOUBLE:
                        o = readReal();
                        break;

                    case Types.SQL_NUMERIC:
                    case Types.SQL_DECIMAL:
                        o = readDecimal(type);
                        break;

                    case Types.SQL_DATE:
                        o = readDate(type);
                        break;

                    case Types.SQL_TIME:
                    case Types.SQL_TIME_WITH_TIME_ZONE:
                        o = readTime(type);
                        break;

                    case Types.SQL_TIMESTAMP:
                    case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
                        o = readTimestamp(type);
                        break;

                    case Types.SQL_INTERVAL_YEAR:
                    case Types.SQL_INTERVAL_YEAR_TO_MONTH:
                    case Types.SQL_INTERVAL_MONTH:
                        o = readYearMonthInterval(type);
                        break;

                    case Types.SQL_INTERVAL_DAY:
                    case Types.SQL_INTERVAL_DAY_TO_HOUR:
                    case Types.SQL_INTERVAL_DAY_TO_MINUTE:
                    case Types.SQL_INTERVAL_DAY_TO_SECOND:
                    case Types.SQL_INTERVAL_HOUR:
                    case Types.SQL_INTERVAL_HOUR_TO_MINUTE:
                    case Types.SQL_INTERVAL_HOUR_TO_SECOND:
                    case Types.SQL_INTERVAL_MINUTE:
                    case Types.SQL_INTERVAL_MINUTE_TO_SECOND:
                    case Types.SQL_INTERVAL_SECOND:
                        o = readDaySecondInterval(type);
                        break;

                    case Types.SQL_BOOLEAN:
                        o = readBoolean();
                        break;

                    case Types.OTHER:
                        o = readOther();
                        break;

                    case Types.SQL_CLOB:
                        o = readClob();
                        break;

                    case Types.SQL_BLOB:
                        o = readBlob();
                        break;

                    case Types.SQL_BINARY:
                    case Types.SQL_VARBINARY:
                        o = readBinary();
                        break;

                    case Types.SQL_BIT:
                    case Types.SQL_BIT_VARYING:
                        o = readBit();
                        break;

                    default:
                        throw Error.runtimeError(ErrorCode.U_S0500,
                                                 "RowInputBase - "
                                                 + type.getNameString());
                }

                data[i] = o;
            }

            return data;
        }

        /**
         *  Used to reset the row, ready for a new row to be written into the
         *  byte[] buffer by an external routine.
         *
         */
        public virtual void resetRow(int filepos, int rowsize)
        {

            _mark = 0;

            reset();

            if (buf.Length < rowsize)
            {
                buf = new byte[rowsize];
            }

            filePos = filepos;
            size = count = rowsize;
            pos = 4;
            buf[0] = (byte)(((uint)rowsize >> 24) & 0xFF);
            buf[1] = (byte)(((uint)rowsize >> 16) & 0xFF);
            buf[2] = (byte)(((uint)rowsize >> 8) & 0xFF);
            buf[3] = (byte)(((uint)rowsize >> 0) & 0xFF);
        }

        public byte[] getBuffer()
        {
            return buf;
        }

        public override int skipBytes(int n)
        {
            throw Error.runtimeError(ErrorCode.U_S0500, "RowInputBase");
        }

        public override String readLine()
        {
            throw Error.runtimeError(ErrorCode.U_S0500, "RowInputBase");
        }
    }

}